package com.liu.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.liu.domain.teacher;
import com.liu.repository.TeacherRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created by liu on 17-9-22.
 */
@RestController
public class TeacherController {
    private final static Logger logger = LoggerFactory.getLogger(TeacherController.class);

    @Autowired
    private TeacherRepository teaRepository;

    /*
    查询所有寝室列表
     */
    @GetMapping(value = "/tea")
    public String teaList(){
        System.out.println("teaList");

        logger.info("teaList");

/*        return teaRepository.findAll();*/
        List<teacher> teachers = teaRepository.findAll();

        return "{\"code\":0,\"msg\":\"\",\"count\":" + teachers.size() + ",\"data\":" + teachers + "}";

    }
    //通过session
    @GetMapping(value = "/tea/sess")
    @ResponseBody
    public teacher sessionById(String tea_id ,HttpSession session) {

        teacher teacher = (com.liu.domain.teacher) session.getAttribute("user");
         String id = teacher.getTea_id();
         System.out.print(tea_id);
        // return stuRepository.findById(stu_id);
        return teaRepository.findById(id);
    }


    //id查询
    @GetMapping(value = "/tea/id/{tea_id}")
    public String studentById(@PathVariable("tea_id") String tea_id) {
        // return stuRepository.findById(stu_id);
        return "{\"code\":0,\"msg\":\"\",\"count\":1000,\"data\":" + teaRepository.findById(tea_id) + "}";
    }


    /*
    添加一个寝室
     */
    @PostMapping(value = "/tea")
    public ModelAndView teacherAdd(teacher tea) {
        teaRepository.save(tea);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("addTea");
        mav.addObject("f", 1);
        return mav;
    }

    /*
    更新一个寝室的信息
     */
    @PostMapping(value = "/tea/update")
    public boolean teacherUpdate(/*teacher tea*/ @RequestBody String date) throws IOException {
        boolean ok = false;
        System.out.println(date);
        ObjectMapper mapper = new ObjectMapper();
        teacher tea = mapper.readValue(date, teacher.class);
        System.out.println(tea);
        teacher t = teaRepository.save(tea);
        if (t != null) {
            ok = true;
        }
        return ok;
    }
    /*
    删除一个寝室
     */

    @DeleteMapping(value = "/teacher/{tea_id}")
    public void teacherDelete(@PathVariable("tea_id") String tea_id){
        teaRepository.delete(tea_id);
    }




    @PostMapping("/checkfomTea")
    public String checkForm(String oldPwd, String newPwd, String reNewPwd, HttpSession session) {
        teacher teacher = (com.liu.domain.teacher) session.getAttribute("user");
        if (!teacher.getPassword().equals(oldPwd)) {
            return "原密码错误";
        }
        if (!reNewPwd.equals(newPwd)) {
            return "两次输入密码不一致";
        }
        teacher.setPassword(newPwd);
        teaRepository.save(teacher);
        return "修改成功";
    }
}
